package com.secondlist;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;

public class HelloWorld{
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame("下拉列表");
    }
}
class MyFrame extends JFrame {

    //    声明下来列表JComboBox
    private JComboBox choice1;
    private JComboBox choice2;
    private JComboBox choice3;

    //定义一个数组
    private static String[] s1 = {"请选择","全部","武器","防具","首饰","特殊装备"};
    private static String[][] s2 = {
            {},
            {},
            {"请选择","全部","鬼剑士/黑暗武士/守护者","格斗家","神枪手","魔法师/缔造者","圣职者","暗夜使者","魔枪士","枪剑士"},
            {"请选择","全部","鬼剑士/黑暗武士/守护者","格斗家","神枪手","魔法师/缔造者","圣职者","暗夜使者","魔枪士","枪剑士"},
            {"请选择","全部","鬼剑士/黑暗武士/守护者","格斗家","神枪手","魔法师/缔造者","圣职者","暗夜使者","魔枪士","枪剑士"},
            {"请选择","全部","鬼剑士/黑暗武士/守护者","格斗家","神枪手","魔法师/缔造者","圣职者","暗夜使者","魔枪士","枪剑士"}
    };
    private static String[][][] s3 = {
            {
                    {}
            },
            {
                    {}
            },
            {       {},
                    {},
                    {"请选择","全部","巨剑","光剑","太刀","短剑","钝器"},
                    {"请选择","全部","拳套","爪","手套","臂铠","东方棍"},
                    {"请选择","全部","手弩","手炮","步枪","自动手枪","左轮手枪"},
                    {"请选择","全部","棍棒","扫把","魔杖","法杖","矛"},
                    {"请选择","全部","战斧","镰刀","念珠","十字架","图腾"},
                    {"请选择","全部","手杖","苦无","双剑","匕首"},
                    {"请选择","全部","战戟","光枪","暗矛","长枪"},
                    {"请选择","全部","重剑","源力剑","小太刀","长刀"}
            },
            {
                    {},
                    {},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"},
                    {"请选择","全部","上衣","下装","腰带","头肩","鞋子"}
            },
            {
                    {},
                    {},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"},
                    {"请选择","全部","称号","项链","手镯","戒指"}
            },
            {
                    {},
                    {},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"},
                    {"请选择","全部","辅助装备","魔法石","耳环"}
            }
    };
    public MyFrame(String title){
        super(title);

        //        设置布局管理
        getContentPane().setLayout(new GridLayout(3,2,0,0));

//        创建一级标签
        JLabel label1 = new JLabel("选择装备类型：");
        label1.setHorizontalAlignment(SwingConstants.RIGHT);
        getContentPane().add(label1);

        //        实例化JComboBox对象
        choice1 = new JComboBox(s1);

        // 为组合框的选择动作注册监听事件,当此组合框的选择有变化时,另一个组合框自动更新内容
        choice1.addActionListener(e -> {
            int index = 0;
            choice2.removeAllItems();
            JComboBox cb = (JComboBox) e.getSource();
//            获得选择项目
            String itemString = (String) cb.getSelectedItem();
            if(itemString != "请选择") {
                System.out.println("choice1 " + itemString);
            };
            index = choice1.getSelectedIndex();
            for(int i=0; i<s2[index].length; i++){
                choice2.addItem(s2[index][i]);
            }
        });
        getContentPane().add(choice1);

        //        创建二级标签
        JLabel label2 = new JLabel("选择职业：");
        label2.setHorizontalAlignment(SwingConstants.RIGHT);
        getContentPane().add(label2);

        //        实例化JComboBox对象
        choice2 = new JComboBox(s2[0]);
        //        注册Action事件监听，采用Lambda表达式
        choice2.addItemListener(e -> {
            int index = 0;
            int index1 = 0;
            choice3.removeAllItems();
            //获得选择项目
            if (e.getStateChange() == ItemEvent.SELECTED){
//                获得选择项目
                String itemString = (String) e.getItem();
                if(itemString != "请选择") {
                System.out.println("choice2 " + itemString);
            }
            }
            index = choice1.getSelectedIndex();
            index1 = choice2.getSelectedIndex();
            if(index1 != -1){
                for(int i=0; i<s3[index][index1].length; i++){
                    choice3.addItem(s3[index][index1][i]);
                }
            }
        });
        getContentPane().add(choice2);

        //        创建三级标签
        JLabel label3 = new JLabel("选择装备：");
        label3.setHorizontalAlignment(SwingConstants.RIGHT);
        getContentPane().add(label3);

        //        实例化JComboBox对象
        choice3 = new JComboBox(s3[0][0]);
        //        注册Action事件监听，采用Lambda表达式
        choice3.addItemListener(e -> {
//            获得选择项目
            if (e.getStateChange() == ItemEvent.SELECTED){
//                获得选择项目
                String itemString = (String) e.getItem();
                if(itemString != "请选择") {
                    System.out.println("choice3 " + itemString);
                }
            }
        });
        getContentPane().add(choice3);

        //        设置窗口大小
        setSize(400,300);
//        设置窗口可见
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//当点击窗口关闭按钮时退出程序
        setLocationRelativeTo(null);//把窗口位置设置到屏幕中间
    }

}


